package com.example.springmvc_demo2.controller;

import com.example.springmvc_demo2.entity.User;
import com.example.springmvc_demo2.service.LoginService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {
    @Autowired
    LoginService loginService;

    @RequestMapping("/login")
    public String openLoginPage() {
        return "login";
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(String username, String password) {
        System.out.println("username:"+username+",password:"+password);
        return "index";}

    //@RequestMapping(value = "/login2",method = RequestMethod.POST)
   // public String login2(User user){
       // System.out.println("username:"+user.getUsername()+",password:"+user.getPassword());
       // return "index";
   // }

    @GetMapping("/login3")
    public String login3(User user){
        System.out.println(user);
        return "index";
    }

    @RequestMapping(value = "/login2", method = RequestMethod.POST)
    public ModelAndView login2(User user, ModelAndView mv){
        System.out.println("username:" + user.getUsername() + ",password:" + user.getPassword());
        if (loginService.login(user) != null) {
            mv.setViewName("redirect:/");
            return mv;
        }
        mv.addObject("msg", "登陆失败");
        mv.setViewName("login");
        return mv;
    }
}

